package com.navinfo.platform;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.net.URISyntaxException;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class GeneratorSql {

    public List<String> readLine(String fileName) {
//        try{
//            FileUtils.readLines(new File(fileName), "utf-8");
//            String path = this.getClass().getResource(fileName).toURI().getPath();
//            BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
//            String data = null;
//            List<String> contents = new ArrayList<>();
//            while((data=reader.readLine()) != null){
//                contents.add(data);
//            }
//            return contents;
//        } catch(Exception e){
//            return Collections.emptyList();
//        }
        return null;
    }

    @Test
    public void generatorSumSql() throws Exception{
        List<String> contents = readLine("/create_table.csv");
        StringBuffer content = new StringBuffer("select ");
        for(String field : contents){
            if(content.length() > 7){
                content.append(", ");
            }

            if(field.equals("cross_cities")){
                content.append("sum(");
                content.append(field);
                content.append(")");
            } else {
                content.append("sum(");
                content.append(field);
                content.append(") ");
                content.append(field);
            }
        }
        content.append(" from dataplatform.vehicle_driving_behavior_info_day group by tid");
        System.out.println(content.toString());
    }

    @Test
    public void generatorCSV() throws IOException {
        File file = new File(".");
        String currPath = file.getAbsolutePath();
        List<String> contents = FileUtils.readLines(new File(currPath + "\\src\\test\\resources\\basic_data.csv"), "utf-8");
        List<String> csvContents = new ArrayList<>();
        for(String field : contents){
            String[] fields = field.split(",");
            System.out.println(fields[0] + "," + FieldNameUtils.camelCaseName(field) + "," + fields[1] + "," + fields[2]);
            csvContents.add(fields[0] + "," + FieldNameUtils.camelCaseName(fields[0]) + "," + fields[1] + "," + fields[2] + ",none,none");
        }
        File targetFile = new File(currPath + "\\src\\test\\resources\\create_table.csv");
        FileUtils.writeLines(targetFile,"UTF-8", csvContents);
    }

    @Test
    public void generatorCode() throws IOException {
        File file = new File(".");
        String currPath = file.getAbsolutePath();
        List<String> contents = FileUtils.readLines(new File(currPath + "\\src\\test\\resources\\create_table.csv"), "utf-8");
        StringBuilder dtoContent =  new StringBuilder("");
        dtoContent.append("package com.navinfo.platform.dto;");
        dtoContent.append("\r\n");
        dtoContent.append("import lombok.Data;");
        dtoContent.append("\r\n");
        dtoContent.append("import java.sql.Timestamp;");
        dtoContent.append("\r\n");
        dtoContent.append("@Data");
        dtoContent.append("\r\n");
        dtoContent.append("public class BasicDataDto {");
        dtoContent.append("\r\n");
        StringBuilder createTableContent = new StringBuilder();
        createTableContent.append("create external table if not exists dataplatform.basic_data_info_day ( ");
        StringBuilder rowParseContent = new StringBuilder();
        for(String field : contents){
            String[] fields = field.split(",");
            String type = fields[2];
            if(type.equals("string")){
                type = "String";
            } else if(type.equals("bigint")){
                type = "Long";
            } else if(type.equals("double")){
                type = "Double";
            } else if(type.equals("int")){
                type = "Integer";
            } else if(type.equals("timestamp")){
                type = "Timestamp";
            }
            dtoContent.append("    /**\r\n");
            dtoContent.append("     * ");
            dtoContent.append(fields[3]);
            dtoContent.append("\r\n");
            dtoContent.append("     */\r\n");
            dtoContent.append("    private ");
            dtoContent.append(type);
            dtoContent.append(" ");
            dtoContent.append(fields[1]);
            dtoContent.append(";");
            dtoContent.append("\r\n");

            rowParseContent.append("basicDataDto.set" + Character.toUpperCase(fields[1].charAt(0)) + fields[1].substring(1) + "(");
            rowParseContent.append(String.format("row.getAs(\"%s\")", fields[0]));
            rowParseContent.append(");\r\n");
        }
        dtoContent.append("}");
        File targetFile = new File(currPath + "\\src\\test\\java\\com\\navinfo\\platform\\dto\\BasicDataDto.java");
        FileUtils.writeStringToFile(targetFile,dtoContent.toString(),"UTF-8");
//        System.out.println(dtoContent.toString());
        System.out.println(rowParseContent.toString());
    }

    @Test
    public void testCalendar(){

        String timeRanges = "1543613490_1543619211,1543623095_1543623255,1543651012_1543652054,1543652303_1543652501,1543655005_1543659951,1543660200_1543666717";
        if(timeRanges != null){
            String[] timeRangeArray = timeRanges.split(",");
            BitSet bitset = new BitSet(24);
//            for(String timeRange : timeRangeArray){
//                String start = timeRange.split("_")[0];
//                String end = timeRange.split("_")[1];
//                Calendar calendar = Calendar.getInstance();
//                calendar.setTimeInMillis(Integer.parseInt(start) * 1000);
//                int startHour = calendar.get(Calendar.HOUR_OF_DAY);
//                calendar.setTimeInMillis(Integer.parseInt(end) * 1000);
//                int endHour = calendar.get(Calendar.HOUR_OF_DAY);
//                System.out.println(startHour);
//                System.out.println(endHour);
//                for(int s = startHour; s <= endHour; s++){
//                    System.out.println("bitset set " + s);
//                    bitset.set(s);
//                }
//            }
            for(int s = 1; s <= 23; s++){
                System.out.println("bitset set " + s);
                bitset.set(s);
            }
            for(int i = 0; i <=25; i++){
                System.out.println(bitset.get(i));
            }
        }


//        String start = "1543613490";
//        String end = "1543619211";
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(Integer.parseInt(start) * 1000);
//        int startHour = calendar.get(Calendar.HOUR_OF_DAY);
//        calendar.setTimeInMillis(Integer.parseInt(end) * 1000);
//        int endHour = calendar.get(Calendar.HOUR_OF_DAY);
////        System.out.println(startHour);
////        System.out.println(endHour);
//        BitSet bitset = new BitSet(24);
//        for(int i = startHour;i<=endHour;i++){
//            bitset.set(i);
//            System.out.println(i);
//        }
//        System.out.println("------------------------------------------------");
//        for(int i = 0; i <23; i++){
//            System.out.println(bitset.get(i));
//        }
    }

    @Test
    public void minValue(){
        List<Integer> ll = Arrays.asList(1,3,5,6,7);
        Integer max = Collections.max(ll);
        Integer min = Collections.min(ll);
        System.out.println(max + "|" + min);

    }

    @Test
    public void generatorAltitudeCode() throws IOException {
        File file = new File(".");
        String currPath = file.getAbsolutePath();
        List<String> contents = FileUtils.readLines(new File(currPath + "\\src\\test\\resources\\altitude"), "utf-8");
//        for(int i = 0; i< 12; i++){
//            System.out.println((i - 1)*500 + "-" + i * 500 + " index = " + i);
//        }
//        System.out.println(5500 + "- max"  + " index = " + 12);
        String[] methods = {"getTime()", "getMileage()", "getFuel()"};
        int i = 0;
        int count = 0;
        for(String str : contents) {
//            String methodSuffix = Character.toUpperCase(str.charAt(0)) + str.substring(1);
//            System.out.println(String.format("basicDataDto.set%s(altitudeDtoMap.get(%d).%s);" ,methodSuffix, i, methods[count++]));
//            if(count == 3){
//                i++;
//                count = 0;
//            }
            System.out.println(FieldNameUtils.underscoreName(str));
        }
    }

    @Test
    public void generatorRow() throws IOException {
        File file = new File(".");
        String currPath = file.getAbsolutePath();
        List<String> contents = FileUtils.readLines(new File(currPath + "\\src\\test\\resources\\create_basic_data.csv"), "utf-8");

        for(String field : contents){
            String fieldName = FieldNameUtils.camelCaseName(field.split(",")[0]);
            String methodSuffix = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);

            System.out.println(String.format("list.add(getIntValue(basicDataDto.get%s()));", methodSuffix));
        }

    }

    @Test
    public void generatorHourInfo(){
        for(int i=0;i<24;i++){
            System.out.println(String.format("vehicle_hour_info.hour%d.time hour%d_time,", i,i));
            System.out.println(String.format("vehicle_hour_info.hour%d.mileage hour%d_mileage,", i,i));
            System.out.println(String.format("vehicle_hour_info.hour%d.fuel hour%d_fuel,", i,i));
        }
    }

    @Test
    public void generatorASLInfo(){
        for(int i=0;i<14;i++){
            System.out.println(String.format("altitude_info.asl%d.time,asl%d_time,", i,i));
            System.out.println(String.format("altitude_info.asl%d.time,asl%d_mileage,", i,i));
            System.out.println(String.format("altitude_info.asl%d.time,asl%d_fuel,", i,i));
        }
    }



}
